package com.yjymm.commonsdk.config;

import com.yjymm.commonsdk.config.interceptor.CrosInterceptor;
import com.yjymm.commonsdk.config.interceptor.RequestLogInterceptor;
import com.yjymm.commonsdk.config.interceptor.UTF8CharsetInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author yjymm
 * @date 2021-01-24 20:42
 */

@Component
@ConditionalOnClass(WebMvcAutoConfiguration.class)
public class WebMvcBaseConfig implements WebMvcConfigurer {

    // 拦截器白名单
    public static String[] LOGIN_WHITE_URL = {
            "/user/login",
            "/user/register",
            "/wx_gateway/openId/**",
            "/user/admin/login",
            "/swagger*",
            "/v2/api-docs",
            "/configuration/ui",
            "/swagger-resources/**",
            "/configuration/security",
            "/swagger-ui.html",
            "/webjars/**"};

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CrosInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new UTF8CharsetInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new RequestLogInterceptor()).addPathPatterns("/**");
    }

    /**
     * 添加新的url白名单
     * @param urls
     */
    public static void addLoginWhileUrl(String[] urls) {
        if (urls == null || urls.length == 0) {
            return;
        }
        String[] a = LOGIN_WHITE_URL;
        int newLength = a.length + urls.length;
        String[] b = new String[newLength];
        System.arraycopy(a, 0, b, 0, a.length);
        System.arraycopy(urls, 0, b, a.length, urls.length);
        LOGIN_WHITE_URL = b;
    }

}
